package everydayGrade.t2;

import java.util.Arrays;

/**
 * @Author: Siantar
 * @Date: 2023-09-07-9:02
 * @Description: 1.0
 */
public class T2594 {
    public long repairCars(int[] ranks, int cars) {
        long left = 1, right = 1l * ranks[0] * cars * cars;
        while (left < right) {
            long mid = (left + right) / 2;
            if (f(ranks, cars, mid)) {
                right = mid;
            } else {
                left = mid+1;
            }
        }
        return left;
    }

    public boolean f(int[] ranks, int cars, long m) {
        long cnt = 0;
        for (int x : ranks) {
            cnt += (long) Math.sqrt(m / x);
        }
        return cnt >= cars;
    }
}
